package com.chuangjiangx.member.manager.client.web.score.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.request.Page;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlinePageResponse;
import com.chuangjiangx.commons.response.UnderlineResponse;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.basic.web.interceptor.Permissions;
import com.chuangjiangx.member.manager.client.web.score.request.ScoreInfoListForMerchantRequest;
import com.chuangjiangx.member.manager.client.web.score.request.ScoreInfoListForScoreRequest;
import com.chuangjiangx.member.manager.client.web.score.response.AppScoreDetailResponse;
import com.chuangjiangx.member.manager.client.web.score.response.ScoreInfoListResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.score.ddd.dal.condition.ScoreStreamCondition;
import com.chuangjiangx.member.score.ddd.query.MbrScoreStreamQuery;
import com.chuangjiangx.member.score.ddd.query.dto.AppScoreDetailDTO;
import com.chuangjiangx.member.score.ddd.query.dto.CashierDetailDTO;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 积分明细
 */
@Api(value = "积分明细" , tags = "积分")
@RestController
@RequestMapping(value = "/app/member/score", produces = "application/json")
public class MbrScoreInfoController {
    @Autowired
    private MbrScoreStreamQuery mbrScoreStreamQuery;

    /**
     * 商户查询积分明细列表
     *
     * @param merchantRequest 查询参数
     * @return 明细列表
     * @map data
     * @see ScoreInfoListResponse
     */
    @ApiOperation(value = "商户查询积分明细列表-商户权限")
    @RequestMapping(value = "/searchScoreInfoListForMerchant",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("55000")
    public UnderlinePageResponse<List<ScoreInfoListResponse>> searchScoreInfoListForMerchant(HttpServletRequest request,
                                                                @Validated @RequestBody ScoreInfoListForMerchantRequest merchantRequest) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        //参数转换
        ScoreStreamCondition condition = new ScoreStreamCondition();
        BeanUtils.convertBean(merchantRequest, condition);

        //获取分页信息
        Page page = new Page();
        page.setPageNO(merchantRequest.getPageNumber());
        page.setEveryPageCount(merchantRequest.getPageSize());

        condition.setPageNumber(merchantRequest.getPageNumber());
        condition.setPageSize(merchantRequest.getPageSize());

        condition.setMerchantId(user.getMerchantId());
        PagingResult<CashierDetailDTO> pagingResult = mbrScoreStreamQuery.getCashierDetailList(condition);
        List<ScoreInfoListResponse> respList = BeanUtils.convertCollection(pagingResult.getItems(), ScoreInfoListResponse.class, null);
        return (UnderlinePageResponse)ResponseUtils.successPage(page, pagingResult, "scoreInfoList", respList);
    }


    /**
     * 根据id查询积分明细详情
     *
     * @param id 积分明细ID
     * @return 积分详情
     * @see AppScoreDetailResponse
     */
    @ApiOperation(value = "根据id查询积分明细详情")
    @RequestMapping(value = "/searchScoreInfo",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    @Login
    @Permissions("59008")
    public UnderlineResponse<AppScoreDetailResponse> searchScoreInfoById(@ApiParam(value = "积分明细ID")@RequestParam(value = "id") Long id) {
        AppScoreDetailResponse response = new AppScoreDetailResponse();
        AppScoreDetailDTO appScoreDetailDTO = mbrScoreStreamQuery.getScoreDetailById(id);
        BeanUtils.convertBean(appScoreDetailDTO, response);
        return (UnderlineResponse<AppScoreDetailResponse>)ResponseUtils.success(response);
    }


    /**
     * 门店查询积分明细列表
     *
     * @param scoreRequest 查询参数
     * @return 明细列表
     * @map data
     * @see ScoreInfoListResponse
     */
    @ApiOperation(value = "门店查询积分明细列表-门店权限")
    @RequestMapping(value = "/searchScoreInfoListForScore",method = {RequestMethod.POST})
    @ResponseBody
    @Login
    @Permissions("56000")
    public UnderlinePageResponse searchScoreInfoListForScore(@RequestBody ScoreInfoListForScoreRequest scoreRequest, HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        ScoreStreamCondition condition = new ScoreStreamCondition();
        BeanUtils.convertBean(scoreRequest, condition);
        //获取分页信息
        Page page = new Page();
        page.setPageNO(scoreRequest.getPageNumber());
        page.setEveryPageCount(scoreRequest.getPageSize());
        condition.setPageNumber(scoreRequest.getPageNumber());
        condition.setPageSize(scoreRequest.getPageSize());
        //以下两项为必传参数，只能查询所属的相关信息
        condition.setRegisterStoreId(user.getStoreId());
        condition.setMerchantId(user.getMerchantId());
        PagingResult<CashierDetailDTO> pagingResult = mbrScoreStreamQuery.getCashierDetailList(condition);
        List<ScoreInfoListResponse> respList = BeanUtils.convertCollection(pagingResult.getItems(), ScoreInfoListResponse.class, null);
        return (UnderlinePageResponse)ResponseUtils.successPage(page, pagingResult, "scoreInfoList", respList);
    }

}
